package com.xsy.android.compose.wandroid.ui.page.login

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.rememberNavController
import com.xsy.android.compose.wandroid.R
import com.xsy.android.compose.wandroid.logic.model.LoginModel
import com.xsy.android.compose.wandroid.logic.model.PlayError
import com.xsy.android.compose.wandroid.logic.model.PlayLoading
import com.xsy.android.compose.wandroid.logic.model.PlayState
import com.xsy.android.compose.wandroid.logic.model.PlaySuccess
import com.xsy.android.compose.wandroid.ui.main.NavigationUtil
import com.xsy.android.compose.wandroid.ui.page.login.view.Email
import com.xsy.android.compose.wandroid.ui.page.login.view.Password
import com.xsy.android.compose.wandroid.ui.page.login.view.SignIn
import com.xsy.android.compose.wandroid.ui.view.TopAppBar
import com.xsy.android.compose.wandroid.ui.view.edit.EmailState
import com.xsy.android.compose.wandroid.ui.view.edit.PasswordState

/**
 *@author: xiaosy
 *时间: 2024/7/4
 *@Description: java类作用描述
 *@Version: 1.0
 **/
sealed class SignInEvent {
    data class SignIn(val email: String, val password: String) : SignInEvent()
    object SignUp : SignInEvent()
    object SignInAsGuest : SignInEvent()
    object NavigateBack : SignInEvent()
}

@Composable
fun LoginPage(
    modifier: Modifier = Modifier,
    onNavigationEvent: NavigationUtil,
    loginState: PlayState<LoginModel>?,
    toLogout: () -> Unit,
    toLoginOrRegister: (Account) -> Unit
) {
    SignIn(onNavigationEvent = { event ->
        when (event) {
            is SignInEvent.SignIn -> {
                toLoginOrRegister(Account(event.email, event.password, true))
            }

            SignInEvent.SignUp -> {
                toLogout()
            }

            SignInEvent.SignInAsGuest -> {
                toLogout()
            }

            SignInEvent.NavigateBack -> {
                onNavigationEvent.upPress
            }
        }
    })
    when (loginState) {
        is PlayLoading -> {

        }

        is PlaySuccess -> {
            onNavigationEvent.upPress()
        }

        is PlayError -> {

        }

        else -> {

        }
    }
}


@Preview(showBackground = true)
@Composable
private fun PreviewLoginPage() {
    val viewModel:LoginViewModel = viewModel()
    val loginState by viewModel.state.observeAsState()
    val navController = rememberNavController()
    val actions = remember(navController) { NavigationUtil(navController) }
    LoginPage(onNavigationEvent = actions, loginState = loginState, toLogout = {
        viewModel.logout()
    }, toLoginOrRegister = {
        viewModel.toLoginOrRegister(it)
    } )
}